package com.word.android.sdk.ad;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.word.android.sdk.WordAdSdk;
import com.word.android.sdk.Constant;
import com.kwai.network.sdk.KwaiAdSDK;
import com.kwai.network.sdk.api.KwaiAdLoaderManager;
import com.kwai.network.sdk.constant.KwaiError;
import com.kwai.network.sdk.loader.business.reward.data.KwaiRewardAd;
import com.kwai.network.sdk.loader.business.reward.data.KwaiRewardAdConfig;
import com.kwai.network.sdk.loader.business.reward.data.KwaiRewardAdRequest;
import com.kwai.network.sdk.loader.business.reward.interf.IKwaiRewardAdListener;
import com.kwai.network.sdk.loader.common.interf.AdLoadListener;
import com.kwai.network.sdk.loader.common.interf.IKwaiAdLoader;

public class WordKwaiReward implements WordVideoIAP {

    private VideoRewardCallback mListner;
    private boolean loadFinish=true;

    private KwaiRewardAd mKwaiRewardAd;
    private IKwaiAdLoader<KwaiRewardAdRequest> mKwaiRewardAdAdLoader = null;

    public static WordKwaiReward getInstance() {
        return InstanceHolder.KWAI_REWARD_VIDEO;
    }

    private static class InstanceHolder {
        private static final WordKwaiReward KWAI_REWARD_VIDEO = new WordKwaiReward();
    }

    public void loadRewardAd() {
        mListner = null;
        if (mKwaiRewardAd != null) {
            if (!mKwaiRewardAd.isReady()) {
                load();
            }
        } else {
            load();
        }
    }

    private void load() {
        if (!loadFinish){
            return;
        }
        // 初始化失败或者未初始化SDK，获取的loaderManager为空
        KwaiAdLoaderManager loaderManager = KwaiAdSDK.getKwaiAdLoaderManager();
        if (loaderManager != null) {
            // 构建一个Loader对象，
            // ps:
            // 1. 一个Loader只能执行一次load
            // 2. 支持预载，只需提前调用loadAd
            mKwaiRewardAdAdLoader = loaderManager.buildRewardAdLoader(
                    new KwaiRewardAdConfig.Builder(new KwaiLoadListener()).withKwaiRewardAdListener(new IKwaiRewardAdListener() {
                        @Override
                        public void onRewardEarned() {

                        }

                        @Override
                        public void onAdShow() {
                        }

                        @Override
                        public void onAdShowFailed(@NonNull KwaiError kwaiError) {
                            Log.d("WTMA","kwai reward onAdShowFailed"+kwaiError.getCode()+"\n"+kwaiError.getMsg());
                            // show error
                            if (mListner != null) {
                                mListner.onAdError();
                            }
                        }

                        @Override
                        public void onAdClick() {

                        }

                        @Override
                        public void onAdClose() {
                            // When the fullsceen ad closes.
                            load();
                            if (mListner != null) {
                                mListner.onAdOver();
                            }
                            mListner=null;
                        }

                        @Override
                        public void onAdPlayComplete() {

                        }
                    }).build()
            );
            // 开始发起请求
//            Log.d("WTMA","kwai reward loading");
            loadFinish=false;
            mKwaiRewardAdAdLoader.loadAd(new KwaiRewardAdRequest(Constant.KWAI_REWARD_PLACEMENTID));
        }

    }

    public void showFullVideo(Activity activity, VideoRewardCallback listner) {
        mListner=listner;
        // 展示之前判断数据是否ready, 数据里的price是ecpm 单位USD
        if (isReady()) {
            mKwaiRewardAd.show(activity);
        }else {
            load();
            if (mListner != null) {
                mListner.onAdError();
            }
        }
    }

    @Override
    public boolean isReady(){
        return mKwaiRewardAd != null && mKwaiRewardAd.isReady();
    }

    @Override
    public void showVideo(Activity activity,VideoRewardCallback listner) {
        showFullVideo(activity, listner);
    }

    public void onDestroy(){
        if (mKwaiRewardAdAdLoader != null) {
            mKwaiRewardAdAdLoader.release();
            mKwaiRewardAdAdLoader = null;
        }
        mListner=null;
    }


    public class KwaiLoadListener implements AdLoadListener<KwaiRewardAd>{

        @Override
        public void onAdLoadStart(@Nullable String s) {

        }

        @Override
        public void onAdLoadSuccess(@Nullable String s, @NonNull KwaiRewardAd kwaiRewardAd) {
            loadFinish=true;
            mKwaiRewardAd = kwaiRewardAd;
            Log.d("WTMA","kwai reward load success");
        }

        @Override
        public void onAdLoadFailed(@Nullable String s, @NonNull KwaiError kwaiError) {
            Log.d("WTMA","kwai reward load error"+kwaiError.getCode()+"\n"+kwaiError.getMsg());
            //load error
            loadFinish=true;
            if (mListner != null) {
                mListner.onAdError();
            }
        }
    }

}
